<!--
  功能：宿舍类型管理
  作者：licy
  邮箱：licy@briup.com
  时间：2020年12月03日 21:51:14
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='dormitoryType_list'>
    <!-- 按钮区域 -->
    <div class="btns">
      <div style="text-align:right">
        <el-button type="primary" size="small" @click="toSaveHandler">新增</el-button>
      </div>
    </div>
    <!-- 按钮区域结束 -->
    <!-- 表格 -->
    <div class="dormitoryTypes">
      <ul>
        <li class="dormitoryType" v-for="d in dormitoryTypes" :key="d.id">
          <div class="photo">
            <img src="@/assets/mt/smart.jpg" alt="" v-if="!d.photos">
            <img v-else :src="d.photos[0].url" alt="">
          </div>
          <div class="right">
            <div class="title">{{d.name}}</div>
            <div class="brief">{{d.address}}</div>
            <div class="price">
              ￥ {{d.price}}
            </div>
            <div class="others">
              <div class="recommand">
                推荐指数 <span style="color:red"> {{d.level}}</span>
              </div>
              <div class="salary">
                第一年薪资水平  <span style="color:red">{{d.salary}}</span>
              </div>
            </div>
          </div>
          <div class="option">
            <div><el-button type="text" size="mini" @click="toDeleteHandler(d)">删除</el-button></div>
            <div><el-button type="text" size="mini" @click="toEditHandler(d)">修改</el-button></div>
          </div>
        </li>
      </ul>
    </div>
    <!-- 表格结束 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="50%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form :model="form" ref="dormitoryTypeForm" :rules="rules" label-width="60px" size="small">
          <div class="sub_title"><i class="el-icon-upload"></i> 基本信息</div>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address"></el-input>
          </el-form-item>
          <el-form-item label="价格" prop="price">
            <el-input v-model="form.price"></el-input>
          </el-form-item>
          <el-form-item label="卫浴" prop="bathroom">
            <el-radio-group v-model="form.bathroom">
              <el-radio label="独立">独立</el-radio>
              <el-radio label="公共">公共</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="网络" prop="internet">
            <el-radio-group v-model="form.internet">
              <el-radio label="wifi">wifi</el-radio>
              <el-radio label="无">无</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="窗户" prop="window">
            <el-radio-group v-model="form.window">
              <el-radio label="有">有</el-radio>
              <el-radio label="无">无</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="面积" prop="area">
            <el-radio-group v-model="form.area">
              <el-radio label="25平米">25平米</el-radio>
              <el-radio label="40平米">40平米</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="床型" prop="bed">
            <el-radio-group v-model="form.bed">
              <el-radio label="1.2×2.0米">1.2×2.0米</el-radio>
              <el-radio label="1.0×2.0米">1.0×2.0米</el-radio>
              <el-radio label="上床下桌">上床下桌</el-radio>
              <el-radio label="上下铺">上下铺</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="sub_title"><i class="el-icon-upload"></i> 房间照片</div>
          <div style="display:flex;flex-direction:row;padding-bottom:1em;flex-wrap:nowrap">
            <div v-for="item in form.photos" :key="item.id" style="flex-basis:24%;margin-right:.5%">
              <img :src="item.url" alt="" style="width:100%">
            </div>
          </div>
          <!-- 修改的时候才能上传 -->
          <div v-if="form.id" style="padding-bottom:3em;text-align:left">
            <el-upload
            class="upload-demo"
            action="http://121.199.29.84:8001/file/upload"
            :on-success="uploadSuccessHandler"
            :file-list="fileList">
            <el-button type="primary" size="mini" round>上传</el-button>
          </el-upload>
          </div>

        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取消</el-button>
        <el-button size="small" type="primary" @click="submitHandler">确定</el-button>
      </span>
    </Briupdrawer>
    <!-- 抽屉结束 -->
  </div>
</template>

<script>
import {get,post} from '@/utils/request'
import _ from 'lodash'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
    visible:false,
    loading:false,
    title:"",
    dormitoryTypes:[],
    fileList:[],
    form:{},
    rules:{
      name: [ { required: true, message: '请输入名称', trigger: 'blur' } ],
      address: [ { required: true, message: '请输入地址', trigger: 'blur' } ],
      price: [ { required: true, message: '请输入价格', trigger: 'blur' } ],
    }
   }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 上传成功
    uploadSuccessHandler(response){
      if(response.status == 200){
        // 上传图片
        let data = {
          dormitoryTypeId:this.form.id,
          url:'http://121.199.29.84:8888/group1/'+response.data.id,
          name:response.data.filename
        }
        // 上传图片
        let url = '/dormitoryTypePhoto/saveOrUpdate'
        post(url,data).then(response => {
          this.$message({type:"success",message:response.message})
          // 刷新
          get('/dormitoryType/findById?id='+this.form.id).then(response =>{
            this.form = response.data;
          })
        })

      } else {
        this.$message({type:'error',message:'附件服务器异常！'})
        this.visible = false;
      }
    },
    // 确认提交处理函数
    submitHandler(){
      // 表单校验
      this.$refs['dormitoryTypeForm'].validate((valid) => {
        if (valid) {
          // 校验通过后，完成数据交互
          let url = '/dormitoryType/saveOrUpdate'
          post(url,this.form).then(response => {
            this.$message({type:"success",message:response.message})
            this.loadDormitoryTypes();
            this.visible = false;
          })
        } else {
          return false;
        }
      });
    },
    // 去保存
    toSaveHandler(){
      this.title = "新增宿舍类型信息";
      this.form = {};
      this.fileList = [];
      this.visible = true;
    },
    // 去修改
    toEditHandler(row){
      this.fileList = [];
      // 修改抽屉标题
      this.title = "修改宿舍类型信息"
      // 将当前行数据的克隆对象赋值给当前form,这样可以避免修改表单数据导致表格数据发生变化。
      this.form = _.clone(row);
      // 显示抽屉
      this.visible = true;
    },
    // 去删除
    toDeleteHandler(row){
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = '/dormitoryType/deleteById'
        get(url,{id:row.id}).then(response =>{
          this.$message({ type: 'success', message: '删除成功!' });
          this.loadDormitoryTypes();
        })
      })
    },
    // 查询宿舍类型信息
    loadDormitoryTypes(){
      // 添加loading,页面加载开始
      this.loading = true;
      let url = "/dormitoryType/findAllWithPhoto"
      get(url).then(response => {
        this.dormitoryTypes = response.data;
        // 页面加载结束
        this.loading = false;
      })
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['dormitoryTypeForm'].clearValidate();
    },
    // 处理visible变化的钩子函数，解决抽屉无法收缩的bug
    handlerVisibleChange(val) {
      this.visible = val
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // 加载宿舍类型信息
    this.loadDormitoryTypes();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped lang="scss">
.dormitoryType_list {
  color: #666;
  font-size: 14px;
  .dormitoryTypes {
    .dormitoryType {
      display: flex;
      flex-direction: row;
      padding:1em 0;
      border-bottom: 1px solid #efefef;
      position: relative;

      .photo {
        flex-basis: 200px;
        height: 120px;
        overflow: hidden;
        border-radius: 5px;
        img {
          width: 100%;
        }
      }
      .right {
        flex:1;
        padding:0 1em;
        .title {
          font-weight: bold;
          line-height: 2em;
        }
        .brief {
          font-size: 12px;
          line-height: 1.5em;
        }
        .others {
          display: flex;
          position: absolute;
          bottom: 2em;
          font-size: 12px;
          & > div {
            margin-right: 1em;
          }
        }
      }
      .option {
        border-left:1px solid #efefef;
        flex-basis: 100px;
        padding-top: 40px;
        text-align: center;
      }
    }
  }
}
</style>